import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { numberToChinese, STData,STRes } from '@delon/abc';
import { UtilService } from '@core/services/util.service';

@Component({
  selector: 'app-qr-code-money-list',
  templateUrl: './qr-code-money-list.component.html',
  styles: []
})
export class QrCodeMoneyListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any[];
  loading = false;
  showTable:boolean=false;
  config: SPage = {
    columns: [
      {
        index: 'date',
        title: '日期',
        width:200,
        // type:ColumnType.tag,
        // filter: {
        //   index:"communityId",
        //   type:'select',
        //   placeholder:"所属社区",
        //   items:[]
        // }
      },
      {
        index: 'areaMoney',
        title: '小区费用(元)',
        fFn:(item,column)=>{
          return item.areaMoney / 100 + "";
        },
        // type:ColumnType.tag,
        statistical: 'sum',
        key: 'areaMoney',
        // filter: {
        //   index:"areaId",
        //   placeholder: '所属小区',
        //   type: 'select',
        //   items:[]
        // }
      },
      {
        index: 'outInMoney',
        title: '出入口费用(元)',
        fFn:(item,column)=>{
          return item.outInMoney / 100 + "";
        },
        type:ColumnType.tag,
        statistical: 'sum',
        key: 'outInMoney',
        // filter: {
        //   placeholder: '支付类型',
        //   type: 'select',
        //   items:[
        //   ]
        // }
      },
      {
        index: 'inStatic',
        title: '合计费用(元)',
        fFn:(item,column)=>{
          let pay1=Number(item.areaMoney);
          let pay2=Number(item.outInMoney);
          return (pay1 + pay2) / 100 +"" ;
        },
        statistical: 'sum',
        key: 'inStatic',
      },
    ],
    showCheckBox: false,
    reqUrl:"/core/qr/pay/paymentStatistics",
    // defaultParam:{"statisticsType":"1","userType":JSON.parse(this.util.getStorage("userNow")).userType}
    defaultParam:{queryType:"date"}
    //data:[]
    // editBtn: [
    //   { title: '编辑' }
    // ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private util:UtilService) { }

  ngOnInit() { 
    //this.getData();
    // this.getArea().then((res)=>{
    //   this.feachCommunity();
    // })
    this.showTable=true;
  }

  dataProcess(data: STData[]){
    console.log(data);
    return data;
  }

  getData(){
    this.http.post(this.api.pay.payList.url,{}).subscribe((res)=>{
      console.log(res);
      this.config.data=res.data;
    })
  }
  _console(e: any): void {
    this.selectData = e;
  }
   //获取小区下拉框数据
   feachCommunity(){
    this.http.post(this.api.area.list.url, {areaName: '',areaRank:6}).subscribe((res: any) => {
     if (res.status == '200') {
       this.config.columns[1].filter.items= res.data.map(item => {
         return {
           label: item.areaName,
           value: item.id
         }
       });
       
     }
   })
 }
 //获取社区下拉框数据
 getArea(){
  return new Promise<void>((resolve) => {
    this.http.post(this.api.area.page.url, {areaRank: '5'}).subscribe((res: any) => {
      console.log(res)
      if (res.status == '200') {
        if(res.data.list.length > 0){
          this.config.columns[0].filter.items= res.data.list.map(item => {
            return {
              label: item.areaName,
              value: item.id
            }
          });
        }
        resolve();
      }
    })
  }) 
 }

  //导出
  export(){
    this.loading=true;
    let body:any={};
    if(this.st.st.req.params != undefined){
      body=this.st.st.req.params;
    }
    body.pageNum=1;
    body.pageSize=this.st.st.total;
    this.http.post(this.api.pay.incomeStatistics.url,body).subscribe(res => {
      this.loading=false;
      if(res.status == 200 && res.data.list.length > 0){
        let data=res.data.list;
         this.st.st.export(data,{
          filename : "收支统计.xlsx"
        })
      }else{
        this.message.error(res.message);
      }
    })
  }

   //按日统计
   staticByDay(){
    // let res=this.getParams(this.st.sf.value);
    // this.st.st.reset({"queryType":"date",...res});
    this.st.st.reset({"queryType":"date"})
  }
  //按月统计
  staticByMonth(){
    // let res=this.getParams(this.st.sf.value);
    // this.st.st.reset({"queryType":"month",...res});
    this.st.st.reset({"queryType":"month"})
  }

  getParams(res){
    let result=res;
    let data:any=this.util.getStorage("userNow");
    data=JSON.parse(data);
    Object.keys(result).forEach(item=>{
      if(Array.isArray(result[item])){
        if(result[item].length != 0){
          result[item]=data.id;
        }else{
          delete result[item];
        }
      }
    })
    return result;
  }

}
